package com.hamizhong.iflowers.web.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import com.hamizhong.iflowers.common.cache.MyCacheFactory;
import com.hamizhong.iflowers.common.cache.MyCacheParam;
import com.hamizhong.iflowers.service.ITestService;
import com.hamizhong.iflowers.web.base.MyBaseAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.TestObjectVO;
import com.hamizhong.iflowers.web.vo.TestVO;
import com.hamizhong.iflowers.web.vo.VarietyVO;

public class TestAction extends MyBaseAction {
	
	private ITestService iTestService;
	
	public ITestService getiTestService() {
		return iTestService;
	}
	public void setiTestService(ITestService iTestService) {
		this.iTestService = iTestService;
	}
	
	@Override
	protected String doRequest() {
		IOPResultDto dto = iTestService.doQueryTestAll();
		List<TestVO> testVOList = null;
		if(dto.isSuccess()) {
			TestObjectVO testObjectVO = (TestObjectVO) dto.getFirstResult();
			testVOList = (List<TestVO>) testObjectVO.getTestVOList();
			logger.info("--------testVOList.size----------:"+testVOList.size());
			for (TestVO testVO : testVOList) {
				logger.info("--------testVOList.name----------:"+testVO.getTestName());
			}
			getRQ().setAttribute("testVOList", testVOList);
		}
		
		//测试缓存读取
		MyCacheParam myTestCacheParam = MyCacheFactory.getInstance().loadCache(MyCacheParam.C_TEST_ATTR);
		Iterator itTest = myTestCacheParam.getKeys();
		List<String[]> myTestTypeAttrList = new ArrayList<String[]>();
		while (itTest.hasNext()) {
			String key = (String) itTest.next();
			String value = (String)myTestCacheParam.getValue(key);
			String[] strArr = {key,value};
			myTestTypeAttrList.add(strArr);
		}
		getRQ().setAttribute("myTestTypeAttrList", myTestTypeAttrList);
		
		//读取数据库缓存
		MyCacheParam myVarietyCacheParam = MyCacheFactory.getInstance().loadCache(MyCacheParam.C_IFLOWERS_VARIETY_DB_ATTR);
		Iterator itVarietyVO = myVarietyCacheParam.getObjValue();
		List<VarietyVO> myVarietyVOList = new ArrayList<VarietyVO>(); 
		try {
			while (itVarietyVO.hasNext()) {
				VarietyVO varietyVO = (VarietyVO) itVarietyVO.next();
				myVarietyVOList.add(varietyVO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Collections.sort(myVarietyVOList, new Comparator<VarietyVO>() {
            public int compare(VarietyVO vo1, VarietyVO vo2) {
                return vo2.getSortId().compareTo(vo1.getSortId());
            }
        });
		getRQ().setAttribute("myVarietyVOList", myVarietyVOList);
		//throw new MyErrorException("my error exception!");
/*		try {
			throw new MyErrorException("my error exception!");
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		return SUCCESS;
	}
	
}
